<?php

/**
 * zestawienie actions.
 *
 * @package    edmuch
 * @subpackage zestawienie
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class osiagnieciaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
     
    $userid = $this->getUser()->getGuardUser()->getId();

    $this->game_results = Doctrine_Core::getTable('GameResult')
      ->createQuery('a')
      ->select("a.*, DATEDIFF(NOW(), a.end_date) AS ile_dni, COUNT(a.score) as ilosc, MAX(a.score) as max_pkt, AVG(a.score) as avg_pkt")
      ->where('a.user_id = ?', $userid)
      ->orderBy('a.end_date DESC')
      ->groupBy('a.games_id')
      ->addGroupBy('a.rozgrywka_id')
      ->limit(6)
      ->execute();
    
    $random = 1; //tu chcialbym random z gier w ktore user_id gral
    
    $this->game_data = Doctrine_Core::getTable('Game')
      ->createQuery('n')
      ->where('n.id = ?', $random)
      ->execute();
    
    $this->global_game_results = Doctrine_Core::getTable('GameResult')
      ->createQuery('g')
      ->select('g.*, MAX(g.score) as max')
      ->where('g.games_id = ?', $random)
      ->orderBy('max DESC')            
      ->groupBy('g.user_id')
      ->limit(3)
      ->execute();
        
    $this->profile = $this->getUser()->getGuardUser()->getProfile();
     
     
    $this->user = $this->getUser()->getGuardUser();
    $this->pager = Doctrine::getTable('sfSocialContact')->getContacts($this->user, 0, 4);
    
    if ($this->is_terapeuta_panel = $this->getUser()->getPermissions()):
        $contacts = $this->terapeuta_pager = Doctrine::getTable('sfSocialContact')->getContacts($this->user);
    
    endif;
    
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new GameResultForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new GameResultForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($game_result = Doctrine_Core::getTable('GameResult')->find(array($request->getParameter('id'))), sprintf('Object game_result does not exist (%s).', $request->getParameter('id')));
    $this->form = new GameResultForm($game_result);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($game_result = Doctrine_Core::getTable('GameResult')->find(array($request->getParameter('id'))), sprintf('Object game_result does not exist (%s).', $request->getParameter('id')));
    $this->form = new GameResultForm($game_result);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($game_result = Doctrine_Core::getTable('GameResult')->find(array($request->getParameter('id'))), sprintf('Object game_result does not exist (%s).', $request->getParameter('id')));
    $game_result->delete();

    $this->redirect('zestawienie/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $game_result = $form->save();

      $this->redirect('zestawienie/edit?id='.$game_result->getId());
    }
  }

}
